//
//  HistoryViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/12.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "HistoryViewController.h"
#import "HistoryTableViewCell.h"
#import "LSZMoveCheckVC.h"
#import "LCHistoryDataManager.h"
#import "LCShengJingTextViewController.h"

@interface HistoryViewController ()

@end

@implementation HistoryViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"历史记录";
    
    self.tableviewHistory = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-49) style:UITableViewStylePlain];
    self.tableviewHistory.backgroundColor = RGBColor(240, 244, 243);
    self.tableviewHistory.delegate = self;
    self.tableviewHistory.dataSource = self;
    //添加以下代码关闭估算行高
    self.tableviewHistory.estimatedRowHeight = 0;
    self.tableviewHistory.estimatedSectionHeaderHeight = 0;
    self.tableviewHistory.estimatedSectionFooterHeight = 0;
    [self.view addSubview:self.tableviewHistory];
    [self.tableviewHistory setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 10)];
    //如果没有占满则清除下面的
    self.tableviewHistory.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableviewHistory registerClass:[HistoryTableViewCell class] forCellReuseIdentifier:@"HistoryTableViewCell"];
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *refreshGifHeader = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 隐藏时间
    refreshGifHeader.lastUpdatedTimeLabel.hidden = YES;
    // 设置header
    self.tableviewHistory.mj_header = refreshGifHeader;

}

-(void)loadNewData{
    [self.tableviewHistory reloadData];
    [self.tableviewHistory.mj_header endRefreshing];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 45;
    }else if(ScreenWidth == 375){
        // 6 7
        return 50;
    }
    else {
        //plus
        return 58;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
#pragma mark - 有几行  这个地方是死的数据   需要调整
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryTableViewCell" forIndexPath:indexPath];
    //此处赋值
    if(indexPath.row == 0 ){
        cell.imageIcon.image =[UIImage imageNamed:@"历史记录_圣经阅读"];
        if (ScreenWidth == 320) {
            //5
            cell.imageIcon.frame = CGRectMake(10,10,34,34);

        }else if(ScreenWidth == 375){
            // 6 7
            cell.imageIcon.frame = CGRectMake(10,10,34*1.2,34*1.2);

        }else {
            //plus
            cell.imageIcon.frame = CGRectMake(10,10,34*1.334,34*1.334);
        }
        CGRect rect = cell.labelRecord.frame;
        rect.origin.x = CGRectGetMaxX(cell.imageIcon.frame)+5;
        cell.labelRecord.frame = rect;

        CGRect rect1 = cell.labelChapter.frame;
        rect1.origin.x = CGRectGetMaxX(cell.imageIcon.frame)+5;
        cell.labelChapter.frame = rect1;

        cell.labelRecord.text = @"圣经阅读记录";
        //这个是获取最近的播放的记录   这个地方是改变的
        cell.labelChapter.text = @"创世纪 第四章";
        if ([LCHistoryDataManager loadBibleTextTitleData]) {
            cell.labelChapter.text = [LCHistoryDataManager loadBibleTextTitleData];
        }else{
            cell.labelChapter.text = @"暂无纪录";
        }
    }else if (indexPath.row == 1){
        cell.imageIcon.image =[UIImage imageNamed:@"历史记录_音频播放记录"];
        if (ScreenWidth == 320) {
            //5
            cell.imageIcon.frame = CGRectMake(10, (45-34)/2, 34, 34);

        }else if(ScreenWidth == 375){
            // 6 7
            cell.imageIcon.frame = CGRectMake(10,(50-34*1.2)/2,34*1.2,34*1.2);

        }else {
            //plus
            cell.imageIcon.frame = CGRectMake(10,(58-34*1.334)/2,34*1.334,34*1.334);
        }
        CGRect rect = cell.labelRecord.frame;
        rect.origin.x = CGRectGetMaxX(cell.imageIcon.frame)+5;
        cell.labelRecord.frame = rect;

        CGRect rect1 = cell.labelChapter.frame;
        rect1.origin.x = CGRectGetMaxX(cell.imageIcon.frame)+5;
        cell.labelChapter.frame = rect1;
        cell.labelRecord.text = @"音频播放记录";
        //这个是获取最近的播放的记录   这个地方是改变的
        if ([LCHistoryDataManager loadTenSongsData] != nil) {
            cell.labelChapter.text = [LCHistoryDataManager loadOneSongsData];
        }else{
            cell.labelChapter.text = @"暂无纪录";
        }
    }else{
        cell.imageIcon.image =[UIImage imageNamed:@"历史记录_视频播放"];
        if (ScreenWidth == 320) {
            //5
            cell.imageIcon.frame = CGRectMake(10, (45-34)/2, 34, 34);

        }else if(ScreenWidth == 375){
            // 6 7
            cell.imageIcon.frame = CGRectMake(10,(50-34*1.2)/2,34*1.2,34*1.2);

        }else {
            //plus
            cell.imageIcon.frame = CGRectMake(10,(58-34*1.334)/2,34*1.334,34*1.334);
        }
        CGRect rect = cell.labelRecord.frame;
        rect.origin.x = CGRectGetMaxX(cell.imageIcon.frame)+5;
        cell.labelRecord.frame = rect;

        CGRect rect1 = cell.labelChapter.frame;
        rect1.origin.x = CGRectGetMaxX(cell.imageIcon.frame)+5;
        cell.labelChapter.frame = rect1;
        cell.labelRecord.text = @"视频播放记录";
        //这个是获取最近的播放的记录   这个地方是改变的
        if ([LCHistoryDataManager loadTenVideosData].count > 0) {
            cell.labelChapter.text = [LCHistoryDataManager loadOneVideosData];
        }else{
            cell.labelChapter.text = @"暂无记录";
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark  - 单元格的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
        if ([LCHistoryDataManager loadBibleTextTitleData]) {
            LCShengJingTextViewController *viewController = [[LCShengJingTextViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
        }

    }else if (indexPath.row == 1){
        NSString *clzName = @"LCHistoryDetailsTableViewController";
        Class clz = NSClassFromString(clzName);//将字符串转化成class
        UIViewController *viewController = [[clz alloc] init];
        if ([LCHistoryDataManager loadTenSongsData]) {
            [self.navigationController pushViewController:viewController animated:YES];
        }
    }else{
        LSZVideoOfflineCacheVC * vc = [[LSZVideoOfflineCacheVC alloc]init];
        vc.isOkOffline = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
@end
